<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: RssService.cfc 1177 2007-06-25 18:20:41Z pfarrell $

Notes:
The vast majority of the RSS functionality is borrowed liberally from Ray Camdent's BlogCFC (http://www.blogcfc.com).
THANKS RAY!!!!
--->
<cfcomponent
	displayname="RssService"
	output="false"
	hint="RssService for MachBlog">
	
	<!---
	PROPERTIES
	--->
	<cfproperty name="entryService" type="org.machblog.entry.EntryService" required="true" />
	<cfproperty name="resourceBundleService" type="org.machblog.i18n.ResourceBundleService" required="true" />
	<cfproperty name="udfs" type="org.machblog.utils.udfs" required="true" />
	
	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="RssService">
		<cfreturn this />
	</cffunction>

	<cffunction name="setEntryService" access="public" output="false" returntype="void">
		<cfargument name="entryService" type="org.machblog.entry.EntryService" required="true" />
		<cfset variables.entryService = arguments.entryService />
	</cffunction>	
	<cffunction name="getEntryService" access="public" output="false" returntype="org.machblog.entry.EntryService">
		<cfreturn variables.entryService />
	</cffunction>
	
	<cffunction name="setResourceBundleService" access="public" output="false" returntype="void">
		<cfargument name="resourceBundleService" type="org.machblog.i18n.ResourceBundleService" required="true" />
		<cfset variables.resourceBundleService = arguments.resourceBundleService />
	</cffunction>
	<cffunction name="getResourceBundleService" access="public" output="false" 
			returntype="org.machblog.i18n.ResourceBundleService">
		<cfreturn variables.resourceBundleService />
	</cffunction>

	<cffunction name="setUdfs" access="public" output="false" returntype="void">
		<cfargument name="udfs" type="org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUdfs" access="public" output="false" returntype="org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>

	<!--- 
	PUBLIC FUNCTIONS
	--->
	<cffunction name="getBlogRss" access="public" output="false" returntype="string" 
			hint="Returns the RSS feed for the entire blog.  By default returns the same entries as displayed on the home page.">
		<cfargument name="numEntries" type="numeric" required="true" />
		<cfargument name="blogTitle" type="string" required="true" />
		<cfargument name="blogUrl" type="string" required="true" />
		<cfargument name="blogDescription" type="string" required="true" />
		<cfargument name="blogLanguage" type="string" required="true" />
		<cfargument name="generator" type="string" required="true" />
		<cfargument name="authorEmail" type="string" required="true" />
		<cfargument name="webmasterEmail" type="string" required="true" />
		<cfargument name="moduleName" type="string" required="true" />
		
		<cfset var entries = getEntryService().getEntries(numToReturn = arguments.numEntries, currentPosts = true) />
		<cfset var publishDate = DateFormat(Now(), "ddd, dd mmm yyyy") & " " & TimeFormat(Now(), "HH:mm:ss") & " -" 
									& NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		<cfset var lastBuildDate = DateFormat(Now(), "ddd, dd mmm yyyy") & " " & TimeFormat(Now(), "HH:mm:ss") & " -" 
									& NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		<cfset var entryDate = "" />
		<cfset var category = "" />
		<cfset var rss = "" />
		
		<cfif entries.recordCount neq 0>
			<cfset lastBuildDate = DateFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries["dt_posted"][1]), 
													"ddd, dd mmm yyyy") & " " & 
										TimeFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries["dt_posted"][1]), 
													"HH:mm:ss") & " -" & 
										NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		</cfif>
		
		<cfsavecontent variable="rss">
		<cfoutput>
			<rss version="2.0">
			<channel>
			<title>#xmlFormat(arguments.blogTitle)#</title>
			<link>#xmlFormat(arguments.blogUrl)#</link>
			<description>#xmlFormat(arguments.blogDescription)#</description>
			<language>#arguments.blogLanguage#</language>
			<pubDate>#publishDate#</pubDate>
			<lastBuildDate>#lastBuildDate#</lastBuildDate>
			<generator>#arguments.generator#</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>#xmlFormat(arguments.authorEmail)#</managingEditor>
			<webMaster>#xmlFormat(arguments.webmasterEmail)#</webMaster>
		<cfloop query="entries">
			<cfset entryDate = DateFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries.dt_posted), 
											"ddd, dd mmm yyyy") & " " & 
								TimeFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries.dt_posted), 
											"HH:mm:ss") & " -" & 
								NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
			<item>
				<title>#xmlFormat(entries.title)#</title>
				<link>#xmlFormat(variables.udfs.buildUrl(arguments.moduleName, 'showEntry', 'entryId=#entries.entry_id#'))#</link>
				<description>
				<!--- Regex operation removes HTML code from blog body output --->
				<cfif Len(REReplaceNoCase(entries.body, "<[^>]*>", "", "ALL")) GTE 250>
				#xmlFormat(Left(REReplace(entries.body, "<[^>]*>", "", "ALL"), 250))#...
				<cfelse>#xmlFormat(REReplace(entries.body, "<[^>]*>", "", "ALL"))#</cfif>
				<cfif Len(entries.more_body)> [more]</cfif>
				</description>
				<cfloop index="category" list="#entries.category_names#">
				<category>#xmlFormat(category)#</category>
				</cfloop>
				<pubDate>#entryDate#</pubDate>
				<guid>#xmlFormat(variables.udfs.buildUrl(arguments.moduleName, 'showEntry', 'entryId=#entries.entry_id#'))#</guid>
				<cfif len(entries.enclosure)>
				<enclosure url="#xmlFormat("#arguments.blogUrl#/uploads/enclosures/#getFileFromPath(entries.enclosure)#")#" length="#entries.enclosure_size#" type="#entries.enclosure_mime_type#"/>
				</cfif>
			</item>
		</cfloop>
			</channel>
			</rss>
		</cfoutput>
		</cfsavecontent>
		
		<cfreturn rss />
	</cffunction>
	
	<cffunction name="getCategoryRss" access="public" output="false" returntype="string" 
			hint="Returns the RSS feed for a particular category.  By default returns the most recent 20 entries.">
		<cfargument name="categoryId" type="uuid" required="true" />
		<cfargument name="categoryName" type="string" required="true" />
		<cfargument name="blogTitle" type="string" required="true" />
		<cfargument name="blogUrl" type="string" required="true" />
		<cfargument name="blogDescription" type="string" required="true" />
		<cfargument name="blogLanguage" type="string" required="true" />
		<cfargument name="generator" type="string" required="true" />
		<cfargument name="authorEmail" type="string" required="true" />
		<cfargument name="webmasterEmail" type="string" required="true" />
		<cfargument name="moduleName" type="string" required="true" />
		
		<cfset var entries = getEntryService().getEntriesByCategory(categoryId = arguments.categoryId, numToReturn = 20) />
		<cfset var publishDate = DateFormat(Now(), "ddd, dd mmm yyyy") & " " & TimeFormat(Now(), "HH:mm:ss") & " -" 
									& NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		<cfset var lastBuildDate = DateFormat(Now(), "ddd, dd mmm yyyy") & " " & TimeFormat(Now(), "HH:mm:ss") & " -" 
									& NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		<cfset var entryDate = "" />
		<cfset var category = "" />
		<cfset var rss = "" />
		
		<cfif entries.recordCount neq 0>
			<cfset lastBuildDate = DateFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries["dt_posted"][1]), 
													"ddd, dd mmm yyyy") & " " & 
										TimeFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries["dt_posted"][1]), 
													"HH:mm:ss") & " -" & 
										NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
		</cfif>

		<cfsavecontent variable="rss">
		<cfoutput>
			<rss version="2.0">
			<channel>
			<title>#xmlFormat(arguments.blogTitle)# - #xmlFormat(arguments.categoryName)#</title>
			<link>#xmlFormat(arguments.blogUrl)#</link>
			<description>#xmlFormat(arguments.blogDescription)#</description>
			<language>#arguments.blogLanguage#</language>
			<pubDate>#publishDate#</pubDate>
			<lastBuildDate>#lastBuildDate#</lastBuildDate>
			<generator>#arguments.generator#</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>#xmlFormat(arguments.authorEmail)#</managingEditor>
			<webMaster>#xmlFormat(arguments.webmasterEmail)#</webMaster>
		<cfloop query="entries">
			<cfset entryDate = DateFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries.dt_posted), 
											"ddd, dd mmm yyyy") & " " & 
								TimeFormat(getResourceBundleService().getLocaleUtils().fromEpoch(entries.dt_posted), 
											"HH:mm:ss") & " -" & 
								NumberFormat(getTimeZoneInfo().utcHourOffset, "00") & "00" />
			<item>
				<title>#xmlFormat(entries.title)#</title>
				<link>#xmlFormat(variables.udfs.buildUrl(arguments.moduleName, 'showEntry', 'entryId=#entries.entry_id#'))#</link>
				<description>
				<!--- Regex operation removes HTML code from blog body output --->
				<cfif Len(REReplaceNoCase(entries.body, "<[^>]*>", "", "ALL")) GTE 250>
				#xmlFormat(Left(REReplace(entries.body, "<[^>]*>", "", "ALL"), 250))#...
				<cfelse>#xmlFormat(REReplace(entries.body, "<[^>]*>", "", "ALL"))#</cfif>
				<cfif Len(entries.more_body)> [more]</cfif>
				</description>
				<cfloop index="category" list="#entries.category_names#">
				<category>#xmlFormat(category)#</category>
				</cfloop>
				<pubDate>#entryDate#</pubDate>
				<guid>#xmlFormat(variables.udfs.buildUrl(arguments.moduleName, 'blog', 'showEntry', 'entryId=#entries.entry_id#'))#</guid>
				<cfif len(entries.enclosure)>
				<enclosure url="#xmlFormat("#arguments.blogUrl#/uploads/enclosures/#getFileFromPath(entries.enclosure)#")#" length="#entries.enclosure_size#" type="#entries.enclosure_mime_type#"/>
				</cfif>
			</item>
		</cfloop>
			</channel>
			</rss>
		</cfoutput>
		</cfsavecontent>
		
		<cfreturn rss />
	</cffunction>

</cfcomponent>